package com.itheima.Service.Impl;

import com.itheima.Service.ServeProjectService;
import com.itheima.mapper.ServeProjectMapper;
import com.itheima.pojo.ServeProject;
import com.itheima.pojo.query.PageQuery;
import com.itheima.pojo.vo.PageResult;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ServeProjectServiceImpl implements ServeProjectService {

    private ServeProjectMapper serveProjectMapper;

    @Override
    public PageResult page(PageQuery<ServeProject> pageQuery) {
        //查询总数量
        long total=serveProjectMapper.count(pageQuery.getBody());
        //分页查询
       List<ServeProject> list= serveProjectMapper.list(pageQuery);
       //计算总页数
        int pages = (int)(total % pageQuery.getPageSize() == 0 ? total / pageQuery.getPageSize() : total / pageQuery.getPageSize() + 1);
        //构建分页对象
        PageResult pageResult = PageResult.builder()
                .currentPage(pageQuery.getPage())
                .total(total)
                .pages(pages)
                .build();
        //放入集合
        pageResult.setData(list);
        return pageResult;
    }
}
